﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Runtime.InteropServices;

using Gyvate.Worldspawn;
using Gyvate.Util;

namespace Gyvate
{
    public partial class GameForm : Form
    {
        private Game m_game;
        Vector2d m_mouseLast = new Vector2d(-1, -1);
        OpenFileDialog m_dialogOpen = new OpenFileDialog();

        public string GetMapName() { return labelMapName.Text; }
        public int GetDifficulty()
        {
            if (radioEasy.Checked)
                return (int)Difficulty.DIFFICULTY_EASY;
            else if (radioNormal.Checked)
                return (int)Difficulty.DIFFICULTY_NORMAL;
            else if (radioHard.Checked)
                return (int)Difficulty.DIFFICULTY_HARD;

            return (int)Difficulty.DIFFICULTY_NORMAL;
        }
        public bool IsSpotLightEnabled() { return checkSpotlight.Checked; }

        public GameForm()
        {
            //AllocConsole();
            InitializeComponent();

            Init();
        }

        private void Init()
        {
            m_game = null;
            this.DoubleBuffered = true;
            this.Width = 640;
            this.Height = 608;
        }

        /*
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool AllocConsole();
        */

        private void renderTimer_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }

        private void OnPaint(object sender, PaintEventArgs e)
        {
            if (m_game != null)
                m_game.RenderWorld(e.Graphics);
        }

        private void KeyBoardDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F12)
            {
                OpenHelp();
                return;
            }

            if (m_game != null)
                    m_game.SendInput(e.KeyCode, false);
        }

        private void KeyBoardUp(object sender, KeyEventArgs e)
        {
            if (m_game != null)
                m_game.SendInput(e.KeyCode, true);
        }

        private void SpawnGame(bool debugMode = false)
        {
            m_game = new Game(this, debugMode);
            timerRender.Enabled = true;
            timerRender.Interval = (int)Math.Floor(1000.0 / Constants.FPS);
        }

        private void CreateNormalGame()
        {
            SetRegularMenuVisible();
            SpawnGame();
        }

        public void KillGame()
        {
            m_game = null;
            SetRegularMenuVisible(true);
        }

        private void CreateDebugGame()
        {
            SetRegularMenuVisible();
            SpawnGame(true);
        }

        private void SetRegularMenuVisible(bool visible = false)
        {
            buttonStart.Visible = buttonStart.Enabled = visible;
            buttonDebug.Visible = buttonDebug.Enabled = visible;
            checkSpotlight.Visible = checkSpotlight.Enabled = visible;
            radioEasy.Visible = radioEasy.Enabled = visible;
            radioNormal.Visible = radioNormal.Enabled = visible;
            radioHard.Visible = radioHard.Enabled = visible;
            groupBox1.Visible = visible;
            label1.Visible = label2.Visible = visible;
            button1.Visible = button1.Enabled = visible;
            button2.Visible = button2.Enabled = visible;
            labelMapName.Visible = visible;
            menuStrip1.Enabled = menuStrip1.Visible = visible;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            CreateNormalGame();
        }

        private void buttonDebug_Click(object sender, EventArgs e)
        {
            CreateDebugGame();
        }

        private void MouseMovement(object sender, MouseEventArgs e)
        {
            if (m_game != null)
                if (m_game.GetUI() != null)
                    m_game.GetUI().SendMouseInputContinuous(e);
        }

        private void MousePressed(object sender, MouseEventArgs e)
        {
            if (m_game != null)
                if (m_game.GetUI() != null)
                    m_game.GetUI().SendMouseInput(e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_dialogOpen.Filter = Constants.MAP_FORMAT;
            m_dialogOpen.Title = "Select a map file...";

            DialogResult result = m_dialogOpen.ShowDialog();

            if (result != DialogResult.OK)
                return;

            labelMapName.Text = m_dialogOpen.FileName;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            labelMapName.Text = "NEPASIRINKTAS";
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            OpenHelp();
        }

        private void OpenHelp()
        {
            Help.ShowHelp(this, Constants.HELP_FILE);
        }
    }
}
